﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 16/04/2009
// Time: 21:32
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterBpr.
	/// </summary>
	public partial class frmRegisterBpr2 : KryptonForm
	{
		public enum BILL_TYPE { TOPAY, TORECEYCE };
		public csharp.rad.finan.entries.frmRegisterBpr2.BILL_TYPE bill_type;
		
		Configuration config = new Configuration();		
		private general.Statuss listStatus 		 = new general.Statuss();
		private CostCenters listCostCenter 		 = new CostCenters();
		private general.Customers listCustomers = new general.Customers();
		private Bills 				  listBills     = new Bills();
		private SubClasss listSubClass 			 = new SubClasss();
		private Classs listClass					 = new Classs();
		private SubGroups listSubgroups			 = new SubGroups();
		private Groups    listGroups				 = new Groups();
		private general.Types collectionType    = new general.Types();
		private general.Types documentType 		 = new general.Types();
		
		private bool mutexGroup;
		private bool mutexSubGroup;
		//private bool mutexClass;		
		private bool mutexSubClass;
		
		public frmRegisterBpr2()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		void BtnGeneratePortionsClick(object sender, EventArgs e)
		{
			generatePortions();
		}
		
		private void generatePortions()
		{
			int numPortions;
			DateTime aux;
			double portionValue, rest, total;
			
			numPortions = Convert.ToInt32(txtNumPortions.Text);
			total = common.Util.formatDouble(txtTotalValue.Text);
			aux = dtpFirstExpiration.Value;
			
			portionValue = Math.Round(total / numPortions, 2);
			rest = Math.Round(total - (portionValue*numPortions), 2);
			
			for(int i=0; i < numPortions; i++)
			{
				//Soma o resto na ultima parcela
				if (i+1 == numPortions)
					portionValue += rest;
				
				dgvPortions.Rows.Add(new String[] {i.ToString(), 
				                     	           (i+1).ToString(),
				                     	           ((i+1)*100).ToString(),
				                     	           aux.ToString("dd/MM/yyyy"),
				                     	           portionValue.ToString("0.00"),
				                     				  "0.00",
				                     				  "0.00",
				                     				  "0.00"});
				aux = aux.AddMonths(1);
			}
		}
		
		private void FrmRegisterBprLoad(object sender, EventArgs e)
		{
			config.deserialize();			
			listStatus.fillList("bpr");
			listCostCenter.fillList();
	   	listBills.fillList();
	   	
	   	documentType.fillList("document");
	   	collectionType.fillList("collection");
	   	
	   	//Fill groups
	   	listGroups.fillList();
	   	finan.Functions.fillComboGroups(listGroups, cmbGroup, "id", "name");
	   	
	   	//Fill subgroups
	   	listSubgroups.fillList();
	   	finan.Functions.fillComboSubGroups(listSubgroups, cmbSubGroup, "id", "name");
	   	
	   	//Fill classes
	   	listClass.fillList();
	   	finan.Functions.fillComboClass(listClass, cmbClass, "id", "name");
	   	
	   	//Fill subclasses
	   	listSubClass.fillList();
	   	finan.Functions.fillComboSubClass(listSubClass, cmbSubClass, "id", "name");			
			
	   	listBills.fillList();
	   	finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
	   	
			fillComboStatus();
			fillComboCostCenter();
			
			fillComboCollectionType();
			fillComboDocumentType();
			
			setDefaultOptions();
		}
		
		private void setDefaultOptions()
		{			
			if (bill_type == BILL_TYPE.TOPAY)
			{
				this.Text = "Contas a Pagar";
				lblCustomer.Text = "Fornecedor";

				Functions.setSelectIndexComboGroup(cmbGroup, config.toPayGroupId);
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, config.toPaySubGroupId);
				Functions.setSelectIndexComboClass(cmbClass, config.toPayClassId);
				Functions.setSelectIndexComboSubClass(cmbSubClass, config.toPaySubClassId);
				Functions.setSelectIndexComboBill(cmbBill, config.toPayBillId);					
			}
			else
			{
				this.Text = "Contas a Receber";
				lblCustomer.Text = "Cliente";
				
				listCustomers.fillListActive();
			   fillComboCustomers();				

				Functions.setSelectIndexComboGroup(cmbGroup, config.toReceiveGroupId);
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, config.toReceiveSubGroupId);
				Functions.setSelectIndexComboClass(cmbClass, config.toReceiveClassId);
				Functions.setSelectIndexComboSubClass(cmbSubClass, config.toReceiveSubClassId);
				Functions.setSelectIndexComboBill(cmbBill, config.toReceiveBillId);					
			}
		}
		
		private void fillComboStatus()
		{
			int tam = listStatus.count();
			for(int i=0; i < tam; i++)
				cmbStatus.Items.Add(listStatus.get(i));
			cmbStatus.DisplayMember = "name";
			cmbStatus.ValueMember = "id";
		}
		
		private void fillComboCostCenter()
		{
			int tam = listCostCenter.count();
			for(int i=0; i < tam; i++)
				cmbCostCenter.Items.Add(listCostCenter.get(i));
			cmbCostCenter.DisplayMember = "name";
			cmbCostCenter.ValueMember = "id";
		}		
		
		private void fillComboCustomers()
		{
			int tam = listCustomers.count();
			for(int i=0; i < tam; i++)
				cmbCustomerName.Items.Add(listCustomers.get(i).person);
			cmbCustomerName.DisplayMember = "name";
			cmbCustomerName.ValueMember = "id";
		}
		
		private void fillComboCollectionType()
		{
			int tam = collectionType.count();
			
			for(int i=0; i < tam; i++)
				cmbCollectionType.Items.Add(collectionType.get(i));
			cmbCollectionType.DisplayMember = "name";
			cmbCollectionType.ValueMember = "id";
		}
		
		private void fillComboDocumentType()
		{
			int tam = documentType.count();
			
			for(int i=0; i < tam; i++)
				cmbDocumentType.Items.Add(documentType.get(i));
			cmbDocumentType.DisplayMember = "name";
			cmbDocumentType.ValueMember = "id";
		}
		
		private void TxtCustomerCodeLeave(object sender, EventArgs e)
		{
			if (bill_type == BILL_TYPE.TOPAY)
			{
				
			}
			else
			{
				searchCustomerByCode(txtCustomerCode.Text);
			}
		}
		
		private void searchCustomerByCode(string code)
		{
			if (listCustomers.searchByCode(code))
				cmbCustomerName.SelectedIndex = listCustomers.getIndex();
		}

		private void CmbGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbGroup.SelectedIndex >= 0)
			{
				if (!mutexGroup)
				{
					listSubgroups.filterByGroup((cmbGroup.SelectedItem as Group).id);
					
					Functions.fillComboSubGroups(listSubgroups, cmbSubGroup, "id", "name");
					
					cmbSubGroup.Text = string.Empty;
					cmbClass.Text = string.Empty;
					cmbSubClass.Text = string.Empty;
					
					listSubgroups.disableFilter();
				}
			}
		}
		
		private void CmbSubGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbSubGroup.SelectedIndex >= 0)
			{
				//fill the combos one level below
				if (!mutexSubGroup)
				{
					listClass.filterBySubGroup((cmbSubGroup.SelectedItem as SubGroup).id);
					
					Functions.fillComboClass(listClass, cmbClass, "id", "name");
					
					cmbClass.SelectedIndex = -1;
					
					listClass.disableFilter();
				}
				
				//set the combo one level above
				mutexGroup = true;
				Functions.setSelectIndexComboGroup(cmbGroup, (cmbSubGroup.SelectedItem as SubGroup).groupId);
				mutexGroup = false;
			}
		}
		
		/*private void CmbClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbClass.SelectedIndex >= 0)
			{
				if (!mutexClass)
				{
					listSubClass.filterByClass((cmbClass.SelectedItem as Class).id);
					
					Functions.fillComboSubClass(listSubClass, cmbSubClass, "id", "name");
					
					cmbSubClass.SelectedIndex = -1;
					
					listSubClass.disableFilter();
				}
				
				mutexSubGroup = true;
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, (cmbClass.SelectedItem as Class).subGroupId);
				mutexSubGroup = false;
			}	
		}
		
		private void CmbSubClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbSubClass.SelectedIndex >= 0)
			{
				if (!mutexSubClass)
				{
					listBills.filterSubClass((cmbSubClass.SelectedItem as SubClass).id);
					//fillDataGridView();
					
					finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");	
					
					cmbBill.SelectedIndex = -1;
					
					listBills.disableFilter();
				}
				
				mutexClass = true;
				Functions.setSelectIndexComboClass(cmbClass, (cmbSubClass.SelectedItem as SubClass).classId);
				mutexClass = false;
			}
		}		
		*/
		
		private void CmbBillSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbBill.SelectedIndex >= 0)
			{
				mutexSubClass = true;
				Functions.setSelectIndexComboBill(cmbBill, (cmbBill.SelectedItem as Bill).subgroupId);
				mutexSubClass = false;
				
				//listBills.fillList((cmbSubClass.SelectedItem as SubClass).id);
				//fillDataGridView();
				
				//finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
			}			
		}
		
		private void BtnSaveClick(object sender, EventArgs e)
		{
			if (common.Util.validateForm(this))
			{				
				if (bill_type == BILL_TYPE.TOPAY)
				{
					config.toPayGroupId 		= (cmbGroup.SelectedItem as Group).id;
					config.toPaySubGroupId 	= (cmbSubGroup.SelectedItem as SubGroup).id;
					config.toPayClassId  	= (cmbClass.SelectedItem as Class).id;
					config.toPaySubClassId  = (cmbSubClass.SelectedItem as SubClass).id;
					config.toPayBillId 		= (cmbBill.SelectedItem as Bill).id;
				}
				else
				{
					config.toReceiveGroupId 	= (cmbGroup.SelectedItem as Group).id;
					config.toReceiveSubGroupId = (cmbSubGroup.SelectedItem as SubGroup).id;
					config.toReceiveClassId  	= (cmbClass.SelectedItem as Class).id;
					config.toReceiveSubClassId = (cmbSubClass.SelectedItem as SubClass).id;
					config.toReceiveBillId 		= (cmbBill.SelectedItem as Bill).id;					
				}
				config.doSerialization();
			}
		}
		
		void TabPrincipalEnter(object sender, EventArgs e)
		{
			txtNumPortions.Focus();
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			
		}
	}
}
